﻿namespace BaseClass.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoDCapNhatGiaBan
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private int _degiaban_cu;
        private int _degiaban_moi;
        private int _degiabansi_cu;
        private int _degiabansi_moi;
        private int _iiddangnhap;
        private int _iidsanpham;
        private string _sngayapgia;
        private string _sngaycn;
        private string _sSchema;
        private string _stenmaytinh;

        public dtoDCapNhatGiaBan()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoDCapNhatGiaBan(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDCapNhatGiaBan.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "delete from ", this._sSchema, ".", tableName, " where 1=1  and ", columnIDSANPHAM, " = ", this._iidsanpham }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDCapNhatGiaBan.cs");
                return false;
            }
        }

        public bool Exits()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Exits(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoDCapNhatGiaBan.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Exits(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { " select  ", columnIDSANPHAM, " from ", this._sSchema, ".", tableName, " where ", columnIDSANPHAM, " = ", this._iidsanpham }), connect).Fill(dataTable);
                return (dataTable.Rows.Count > 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoDCapNhatGiaBan.cs");
                return false;
            }
        }

        public int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtoDCapNhatGiaBan.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = Convert.ToInt32(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtoDCapNhatGiaBan.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDCapNhatGiaBan.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnIDSANPHAM, ",", columnNGAYAPGIA, ",", columnGIABANLE_CU, ",", columnGIABANLE_MOI, ",", columnGIABANSI_CU, ",", columnGIABANSI_MOI, 
                    ",", columnIDDANGNHAP, ",", columnTENMAYTINH, ") values(", this._iidsanpham, ",to_timestamp('", this._sngayapgia, "','dd/mm/yyyy hh24:mi'),", this._degiaban_cu, ",", this._degiaban_moi, ",", this._degiabansi_cu, ",", this._degiabansi_moi, 
                    ",", this._iiddangnhap, ",:", columnTENMAYTINH, ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 50).Value = this._stenmaytinh;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDCapNhatGiaBan.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._iidsanpham = 0;
            this._sngayapgia = "";
            this._degiaban_cu = 0;
            this._degiaban_moi = 0;
            this._degiabansi_cu = 0;
            this._degiabansi_moi = 0;
            this._iiddangnhap = 0;
            this._stenmaytinh = "";
            this._sngaycn = "";
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoDCapNhatGiaBan.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { 
                    " select  ", columnIDSANPHAM, ",to_char(", columnNGAYAPGIA, ",'dd/mm/yyyy hh24:mi') as ", columnNGAYAPGIA, ",", columnGIABANLE_CU, ",", columnGIABANLE_MOI, ",", columnGIABANSI_CU, ",", columnGIABANSI_MOI, ",", columnIDDANGNHAP, 
                    ",", columnTENMAYTINH, ",to_char(", columnNGAYCN, ",'dd/mm/yyyy hh24:mi:ss') as ", columnNGAYCN, " from ", this._sSchema, ".", tableName, " where ", columnIDSANPHAM, " = ", this._iidsanpham
                 }), connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._iidsanpham = Convert.ToInt32(dataTable.Rows[0][columnIDSANPHAM].ToString());
                    this._sngayapgia = dataTable.Rows[0][columnNGAYAPGIA].ToString();
                    this._degiaban_cu = Convert.ToInt32(dataTable.Rows[0][columnGIABANLE_CU].ToString());
                    this._degiaban_moi = Convert.ToInt32(dataTable.Rows[0][columnGIABANLE_MOI].ToString());
                    this._degiabansi_cu = Convert.ToInt32(dataTable.Rows[0][columnGIABANSI_CU].ToString());
                    this._degiabansi_moi = Convert.ToInt32(dataTable.Rows[0][columnGIABANSI_MOI].ToString());
                    this._iiddangnhap = Convert.ToInt32(dataTable.Rows[0][columnIDDANGNHAP].ToString());
                    this._stenmaytinh = dataTable.Rows[0][columnTENMAYTINH].ToString();
                    this._sngaycn = dataTable.Rows[0][columnNGAYCN].ToString();
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoDCapNhatGiaBan.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoDCapNhatGiaBan.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnIDSANPHAM + ",to_char(" + columnNGAYAPGIA + ",'dd/mm/yyyy hh24:mi') as " + columnNGAYAPGIA + "," + columnGIABANLE_CU + "," + columnGIABANLE_MOI + "," + columnGIABANSI_CU + "," + columnGIABANSI_MOI + "," + columnIDDANGNHAP + "," + columnTENMAYTINH + ",to_char(" + columnNGAYCN + ",'dd/mm/yyyy hh24:mi:ss') as " + columnNGAYCN + " from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoDCapNhatGiaBan.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDCapNhatGiaBan.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnNGAYAPGIA, " = to_timestamp('", this._sngayapgia, "','dd/mm/yyyy hh24:mi'),", columnGIABANLE_CU, " = ", this._degiaban_cu, ",", columnGIABANLE_MOI, " = ", this._degiaban_moi, 
                    ",", columnGIABANSI_CU, " = ", this._degiabansi_cu, ",", columnGIABANSI_MOI, " = ", this._degiabansi_moi, ",", columnIDDANGNHAP, " = ", this._iiddangnhap, ",", columnTENMAYTINH, " = :", columnTENMAYTINH, 
                    ",", columnNGAYCN, " = now()  where ", columnIDSANPHAM, " = ", this._iidsanpham
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDCapNhatGiaBan.cs");
                return false;
            }
        }

        public static string columnGIABANLE_CU
        {
            get
            {
                return "giaban_cu";
            }
        }

        public static string columnGIABANLE_MOI
        {
            get
            {
                return "giaban_moi";
            }
        }

        public static string columnGIABANSI_CU
        {
            get
            {
                return "giabansi_cu";
            }
        }

        public static string columnGIABANSI_MOI
        {
            get
            {
                return "giabansi_moi";
            }
        }

        public static string columnIDDANGNHAP
        {
            get
            {
                return "iddangnhap";
            }
        }

        public static string columnIDSANPHAM
        {
            get
            {
                return "idsanpham";
            }
        }

        public static string columnNGAYAPGIA
        {
            get
            {
                return "ngayapgia";
            }
        }

        public static string columnNGAYCN
        {
            get
            {
                return "ngaycn";
            }
        }

        public static string columnTENMAYTINH
        {
            get
            {
                return "tenmaytinh";
            }
        }

        public int pGIABANLE_CU
        {
            get
            {
                return this._degiaban_cu;
            }
            set
            {
                this._degiaban_cu = value;
            }
        }

        public int pGIABANLE_MOI
        {
            get
            {
                return this._degiaban_moi;
            }
            set
            {
                this._degiaban_moi = value;
            }
        }

        public int pGIABANSI_CU
        {
            get
            {
                return this._degiabansi_cu;
            }
            set
            {
                this._degiabansi_cu = value;
            }
        }

        public int pGIABANSI_MOI
        {
            get
            {
                return this._degiabansi_moi;
            }
            set
            {
                this._degiabansi_moi = value;
            }
        }

        public int pIDDANGNHAP
        {
            get
            {
                return this._iiddangnhap;
            }
            set
            {
                this._iiddangnhap = value;
            }
        }

        public int pIDSANPHAM
        {
            get
            {
                return this._iidsanpham;
            }
            set
            {
                this._iidsanpham = value;
            }
        }

        public string pNGAYAPGIA
        {
            get
            {
                return this._sngayapgia;
            }
            set
            {
                this._sngayapgia = value;
            }
        }

        public string pNGAYCN
        {
            get
            {
                return this._sngaycn;
            }
        }

        public string pTENMAYTINH
        {
            get
            {
                return this._stenmaytinh;
            }
            set
            {
                this._stenmaytinh = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "d_capnhatgiaban";
            }
        }
    }
}

